using System;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Media;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;

using TeleTwitter.Lib;

using WpfTeleTwitter.Properties;

namespace WpfTeleTwitter
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();

			Loaded += WindowOnLoaded;
		}

		private void WindowOnLoaded(object sender, RoutedEventArgs e)
		{
			if (String.IsNullOrEmpty(Settings.Default.UserName) || String.IsNullOrEmpty(Settings.Default.Password))
			{
				if (!RequestCredentials())
					Close();
			}

			bool loggedIn;
			do
			{
				StatusManager.UserName = String.Empty;
				StatusManager.Password = Settings.Default.Password;
				StatusManager.UserName = Settings.Default.UserName;
				loggedIn = StatusManager.Login();
				if (!loggedIn)
				{
					if (!RequestCredentials())
					{
						Close();
						return;
					}
				}
			}
			while (!loggedIn);

			InitTwitter();
		}

		private bool RequestCredentials()
		{
			LoginDialog dlg = new LoginDialog();
			dlg.UserName = Settings.Default.UserName;
			dlg.Password = Settings.Default.Password;
			dlg.Owner = this;
			Nullable<bool> dialogResult = dlg.ShowDialog();
			if (dialogResult.GetValueOrDefault(false))
			{
				Settings.Default.UserName = dlg.UserName;
				Settings.Default.Password = dlg.Password;
				Settings.Default.Save();
				return true;
			}
			else
				return false;
		}

		private void InitTwitter()
		{
			Timeline t = new Timeline();
			t.TimelineName = "Friends";
			t.ThisTimelineType = TimelineType.AllFriends;
			StatusList statusList = StatusManager.Add(t);

			statusList.ListStatusChanged += StatusListOnListStatusChanged;
			statusList.CollectionChanged += StatusListOnCollectionChanged;

			myListBox.ItemsSource = CreateView(statusList);

			StatusManager.Start();

			myTextBox.KeyDown += newStatusTextBox_Key;
		}

		private void PublicReplyButtonOnClick(object sender, RoutedEventArgs e)
		{
			object o = ((Button)sender).Tag;
			System.Diagnostics.Debugger.Break();
		}

		private void DirectReplyButtonOnClick(object sender, RoutedEventArgs e)
		{
			object o = ((Button)sender).Tag;
			System.Diagnostics.Debugger.Break();
		}

		private void StatusListOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			if (e.Action == NotifyCollectionChangedAction.Add)
			{
				SystemSounds.Question.Play();
			}
		}

		private void newStatusTextBox_Key(object sender, KeyEventArgs e)
		{
			if (e.Key == Key.Enter)
			{
				if (myTextBox.Text.Length > 2)
				{
					StatusManager.UpdateStatus(myTextBox.Text);
					myTextBox.Clear();
				}
			}
		}

		private static void StatusListOnListStatusChanged(object sender, ListStatusChangedEventArgs e)
		{
			if (e.ListStatus == ListStatus.Error)
			{
				SystemSounds.Exclamation.Play();
			}
			else
			{
				SystemSounds.Beep.Play();
			}
		}

		private ListCollectionView CreateView(StatusList statusList)
		{
			IList list = new DispatchingCollection<StatusList, Status>(statusList, Dispatcher);
			ListCollectionView colView = new ListCollectionView(list);
			colView.SortDescriptions.Add(new SortDescription("CreatedAt", ListSortDirection.Descending));
			return colView;
		}
	}
}